package com.example.gzysmartcity.viewmodel;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.example.gzysmartcity.repository.model.Commodity;
import com.example.gzysmartcity.repository.model.ConsumptionWebInfo;
import com.example.gzysmartcity.repository.model.Person;
import com.example.gzysmartcity.repository.model.PersonBuyInfo;
import com.example.gzysmartcity.repository.model.PersonInInfo;
import com.example.gzysmartcity.repository.model.PersonLike;
import com.example.gzysmartcity.repository.model.PersonOrder;
import com.example.gzysmartcity.repository.model.PersonVip;
import com.example.gzysmartcity.utils.NetworkUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ExpendViewModel extends ViewModel {
    private final List<ConsumptionWebInfo> list = new ArrayList<>();
    private final List<ConsumptionWebInfo> list2 = new ArrayList<>();
    private final List<ConsumptionWebInfo> list3 = new ArrayList<>();
    private final MutableLiveData<Integer> mutableLiveData = new MutableLiveData<>();
    private List<PersonBuyInfo.DataBean> personBuyInfoList;
    private List<PersonLike.DataBean> personLikeList;
    private List<PersonVip.DataBean> personVipList;
    private List<Person.DataBean> personList;
    private List<PersonOrder.DataBean> personOrderList;

    private void getPerson() {
        ThreadUtils.executeByCached(new ThreadUtils.Task<Person>() {
            @Override
            public Person doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("Person/getAll?", null, Person.class, 0);
            }

            @Override
            public void onSuccess(Person result) {
                ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
                    @Override
                    public Object doInBackground() {
                        personList = result.getData();
                        Map<Integer, Float> map = new HashMap<>();
                        for (PersonBuyInfo.DataBean dataBean : personBuyInfoList) {
                            int personId = dataBean.getPersonId();
                            Float aFloat = map.get(personId);
                            if (aFloat == null) {
                                aFloat = 0f;
                            }
                            map.put(personId, aFloat + dataBean.getNum());
                        }
                        for (Map.Entry<Integer, Float> entry : map.entrySet()) {
                            list2.add(new ConsumptionWebInfo(personList.stream().filter(a -> a.getId() == entry.getKey()).findFirst().get().getName(),
                                    entry.getValue()));
                        }
                        return null;
                    }

                    @Override
                    public void onSuccess(Object result) {
                        mutableLiveData.setValue(2);
                    }
                });
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }

    public void getSales() {
        ThreadUtils.executeByCached(new ThreadUtils.Task<PersonBuyInfo>() {
            @Override
            public PersonBuyInfo doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("PersonBuyInfo/getAll?", null, PersonBuyInfo.class, 0);
            }

            @Override
            public void onSuccess(PersonBuyInfo result) {
                personBuyInfoList = result.getData();
                getType();
                getPerson();
                getPersonLike();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }

    private void getType() {
        ThreadUtils.executeByCached(new ThreadUtils.Task<Commodity>() {
            @Override
            public Commodity doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("Commodity/getAll?", null, Commodity.class, 0);
            }

            @Override
            public void onSuccess(Commodity result) {
                List<Commodity.DataBean> data = result.getData();
                Map<Integer, Integer> map = new HashMap<>();
                for (PersonBuyInfo.DataBean dataBean : personBuyInfoList) {
                    int commodityId = dataBean.getCommodityId();
                    Integer integer = map.get(commodityId);
                    if (integer == null) {
                        Log.i("TAG", "onSuccess: ");
                        integer = 0;
                    }
                    map.put(commodityId, integer + dataBean.getNum());
                }
                for (Map.Entry<Integer, Integer> integerDoubleEntry : map.entrySet()) {
                    list3.add(new ConsumptionWebInfo(data.stream().filter(a -> a.getId() == integerDoubleEntry.getKey()).findFirst().get().getCommodityName(),
                            integerDoubleEntry.getValue()));
                }
                mutableLiveData.setValue(3);
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }

    private void getPersonLike() {
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<PersonLike>() {
            @Override
            public PersonLike doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("PersonLike/getAll?", null, PersonLike.class, 0);
            }

            @Override
            public void onSuccess(PersonLike result) {
                personLikeList = result.getData();
                getPersonVip();
            }
        });
    }

    private void getPersonVip() {
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<PersonVip>() {
            @Override
            public PersonVip doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("PersonVip/getAll?", null, PersonVip.class, 0);
            }

            @Override
            public void onSuccess(PersonVip result) {
                personVipList = result.getData();
                getPersonOrder();
            }
        });
    }

    private void getPersonOrder() {
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<PersonOrder>() {
            @Override
            public PersonOrder doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("PersonOrder/getAll?", null, PersonOrder.class, 0);
            }

            @Override
            public void onSuccess(PersonOrder result) {
                personOrderList = result.getData();
                getPersonInInfo();
            }
        });
    }

    private void getPersonInInfo() {
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<PersonInInfo>() {
            @Override
            public PersonInInfo doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("PersonInInfo/getAll?", null, PersonInInfo.class, 0);
            }

            @Override
            public void onSuccess(PersonInInfo result) {
                ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
                    @Override
                    public Object doInBackground() {
                        //    private List<PersonBuyInfo.DataBean> personBuyInfoList;
                        //    private List<PersonLike.DataBean> personLikeList;
                        //    private List<PersonVip.DataBean> personVipList;
                        //    private List<Person.DataBean> personList;
                        //    private List<PersonOrder.DataBean> personOrderList;
                        List<PersonInInfo.DataBean> personInInfoList = result.getData();
                        int allPersonSize = personList.size();

                        Set<Integer> set = new HashSet<>();
                        for (PersonLike.DataBean dataBean : personLikeList) {
                            set.add(dataBean.getPersonId());
                        }
                        int likeSize = set.size();
                        set.clear();

                        for (PersonVip.DataBean dataBean : personVipList) {
                            set.add(dataBean.getPersonId());
                        }
                        int vipSize = set.size();
                        set.clear();

                        for (PersonInInfo.DataBean dataBean : personInInfoList) {
                            set.add(dataBean.getPersonId());
                        }
                        int inSize = set.size();
                        set.clear();

                        for (PersonOrder.DataBean dataBean : personOrderList) {
                            set.add(dataBean.getPersonId());
                        }
                        int orderSize = set.size();
                        set.clear();

                        for (PersonBuyInfo.DataBean dataBean : personBuyInfoList) {
                            set.add(dataBean.getPersonId());
                        }
                        int buySize = set.size();
                        set.clear();
                        LogUtils.i(allPersonSize + " " + likeSize + " " + vipSize + " " + inSize + " " + orderSize + " " + buySize);
                        list.add(new ConsumptionWebInfo(likeSize * 2.5f, "顾客喜好"));
                        list.add(new ConsumptionWebInfo(vipSize * 2.5f, "顾客会员"));
                        list.add(new ConsumptionWebInfo(inSize * 2.5f, "进店记录"));
                        list.add(new ConsumptionWebInfo(orderSize * 2.5f, "购买订单"));
                        list.add(new ConsumptionWebInfo(buySize * 2.5f, "购买记录"));
                        return null;
                    }

                    @Override
                    public void onSuccess(Object result) {
                        mutableLiveData.setValue(1);
                    }
                });
            }
        });
    }

    public List<PersonBuyInfo.DataBean> getPersonBuyInfoList() {
        return personBuyInfoList;
    }

    public List<ConsumptionWebInfo> getList() {
        return list;
    }

    public List<ConsumptionWebInfo> getList2() {
        return list2;
    }

    public List<ConsumptionWebInfo> getList3() {
        return list3;
    }

    public MutableLiveData<Integer> getMutableLiveData() {
        return mutableLiveData;
    }

}
